﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;


namespace Ktra_Tin.BL
{
    class BLThueDia
    {
        Form frm;
        Ktra_Tin.DA.AccesData con;
        DataTable tb;

        public BLThueDia(Ktra_Tin.GUI.frm_thuedia f)
        {
            con = new Ktra_Tin.DA.AccesData();
            frm = f;
        }

        public void Refresh()
        {
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;

            ((ComboBox)frm.Controls["cb_makh"]).DataBindings.Clear();
            ((ComboBox)frm.Controls["cb_macd"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_soluongmuon"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_ngaymuon"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_ngaytra"]).DataBindings.Clear();

            ((ComboBox)frm.Controls["cb_makh"]).DataBindings.Add("Text",tb,"MaKH");
            ((ComboBox)frm.Controls["cb_macd"]).DataBindings.Add("Text",tb,"MaCD");
            ((TextBox)frm.Controls["txt_soluongmuon"]).DataBindings.Add("Text",tb,"SoLuongMuon");
            ((TextBox)frm.Controls["txt_ngaymuon"]).DataBindings.Add("text",tb,"NgayMuon");
            ((TextBox)frm.Controls["txt_ngaytra"]).DataBindings.Add("Text", tb, "NgayTra");

        }

        public void cb_MaKH()
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaKH", DBNull.Value);
            DataTable tb = con.Execute_ps("ps_LayKhachHang", pr);
            ComboBox cb = ((ComboBox)frm.Controls["cb_makh"]);
            cb.DataSource = tb;
            cb.DisplayMember = "TenKH";
           
            cb.ValueMember = "MaKH";
            con.Close();
        }

        public void cb_MaCD()
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaCD", DBNull.Value);
            DataTable tb = con.Execute_ps("ps_layCD", pr);
            ComboBox cb = ((ComboBox)frm.Controls["cb_macd"]);
            cb.DataSource = tb;
            cb.DisplayMember = "TenLoai";
            cb.ValueMember = "MaCD";
            con.Close();
        }

        public void hienthi(string MaKH,string MaCD)
        {
            SqlParameter[] pr = new SqlParameter[2];
            if (MaKH != null && MaCD!=null)
            {
                pr[0] = new SqlParameter("@MaKH", MaKH);
                pr[1] = new SqlParameter("@MaCD", MaCD);
            }
            else
            {
                pr[0] = new SqlParameter("@MaKH", DBNull.Value);
                pr[1] = new SqlParameter("@MaCD", DBNull.Value);
            }
            tb = con.Execute_ps("ps_laythuedia", pr);
            Refresh();

        }

        public void them()
        {
            string str_MaKH = ((ComboBox)frm.Controls["cb_makh"]).SelectedValue.ToString();
            string str_MaCD = ((ComboBox)frm.Controls["cb_macd"]).SelectedValue.ToString();
            string str_soluongmuon = ((TextBox)frm.Controls["txt_soluongmuon"]).Text;
            string str_ngaymuon = ((TextBox)frm.Controls["txt_ngaymuon"]).Text;
            string str_ngaytra = ((TextBox)frm.Controls["txt_ngaytra"]).Text;
            con.Open();
            SqlParameter[] pr = { new SqlParameter("MaKH", str_MaKH), new SqlParameter("MaCD", str_MaCD), new SqlParameter("SoLuongMuon", str_soluongmuon), new SqlParameter("NgayMuon", str_ngaymuon), new SqlParameter("NgayTra", str_ngaytra) };
            tb = con.Execute_ps("ps_ThemThueDia",pr);
            Refresh();
            con.Close();

        }

        public void sua()
        {
            string str_MaKH = ((ComboBox)frm.Controls["cb_makh"]).Text;
            string str_MaCD = ((ComboBox)frm.Controls["cb_macd"]).Text;
            string str_soluongmuon = ((TextBox)frm.Controls["txt_soluongmuon"]).Text;
            string str_ngaymuon = ((TextBox)frm.Controls["txt_ngaymuon"]).Text;
            string str_ngaytra = ((TextBox)frm.Controls["txt_ngaytra"]).Text;
            con.Open();
            SqlParameter[] pr = { new SqlParameter("MaKH", str_MaKH), new SqlParameter("MaCD", str_MaCD), new SqlParameter("SoLuongMuon", str_soluongmuon), new SqlParameter("NgayMuon", str_ngaymuon), new SqlParameter("NgayTra", str_ngaytra) };
            tb = con.Execute_ps("ps_SuaThueDia", pr);
            Refresh();
            con.Close();

        }

        public void xoa()
        {
            con.Open();
            string str_MaKH = ((ComboBox)frm.Controls["cb_makh"]).Text;
            string str_MaCD = ((ComboBox)frm.Controls["cb_macd"]).Text;
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter("@MaKH", str_MaKH);
            pr[1] = new SqlParameter("@MaCD", str_MaCD);
            tb = con.Execute_ps("ps_XoathueDia", pr);
            //((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
            Refresh();
            con.Close();
        }

    }
}
